﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EasyClassUI.entity;
using EasyClassUI.WebRequest;

namespace EasyClassUI.forms.main
{
    public partial class LoginWindow : Form
    {
        public LoginWindow()
        {
            InitializeComponent();
        }

        private void signUpButton_Click(object sender, EventArgs e)
        {
            if (switchButton.Text == "Sign up")  //sign in -> sign up
            {
                nameLabel.Visible = true;
                nameTextBox.Visible = true;
                cueLabel.Text = "Already got an account?";
                switchButton.Text = "Login";
                enterButton.Text = "Sign up";
                confirmPwdTextBox.Visible = true;
                confirmPwdLabel.Visible = true;
                departComboBox.Visible = true;
                departLabel.Visible = true;
            }
            else
            {
                nameLabel.Visible = false;
                nameTextBox.Visible = false;
                cueLabel.Text = "Don't have an account?";
                switchButton.Text = "Sign up";
                enterButton.Text = "Login";
                confirmPwdTextBox.Visible = false;
                confirmPwdLabel.Visible = false;
                departComboBox.Visible = false;
                departLabel.Visible = false;
            }
        }

        private void changeToLogin() {
            nameLabel.Visible = false;
            nameTextBox.Visible = false;
            cueLabel.Text = "Don't have an account?";
            switchButton.Text = "Sign up";
            enterButton.Text = "Login";
            confirmPwdTextBox.Visible = false;
            confirmPwdLabel.Visible = false;
            departComboBox.Visible = false;
            departLabel.Visible = false;
        }

        private void LoginWindow_Load(object sender, EventArgs e)
        {
            nameLabel.Visible = false;
            nameTextBox.Visible = false;
            cueLabel.Text = "Don't have an account?";
            switchButton.Text = "Sign up";
            enterButton.Text = "Login";
            errorMessageLabel.Visible = false;
            confirmPwdLabel.Visible = false;
            confirmPwdTextBox.Visible = false;
            departComboBox.Visible = false;
            departLabel.Visible = false;
        }

        private void enterButton_Click(object sender, EventArgs e)
        {
            if (enterButton.Text == "Login")
            {
                User userToLogin = new User() { UserAccount = iDTextBox.Text, Password = pwdTextBox.Text };
                AccountService accountService = new AccountService();
                LoginStatus LoginStatus = accountService.Login(userToLogin);
                if (LoginStatus.StatusCode == "200") {
                    this.Hide();
                    User.CurrentUser = userToLogin;
                    User.CurrentUser.Token = LoginStatus.Token;
                    MainWindow mainWindow = new MainWindow();
                    mainWindow.Show();
                }

                else if (LoginStatus.StatusCode == "400") {
                    errorMessageLabel.Visible = true;
                    errorMessageLabel.Text = "Login Failed, something went wrong. Check your account and password.";
                }

                else {
                    errorMessageLabel.Visible = true;
                    errorMessageLabel.Text = "Check your network connection.";
                }
            }

            else
            {
                User userToRegister = new User() { UserAccount = iDTextBox.Text, Department = departComboBox.Text, UserName = nameTextBox.Text, Password = pwdTextBox.Text };
                AccountService accountService = new AccountService();
                string result = accountService.Register(userToRegister);
                if (result == "200") {
                    errorMessageLabel.Visible = true;
                    errorMessageLabel.Text = "Registration successed!";
                    changeToLogin();
                }

                else if (result == "400") {
                    errorMessageLabel.Visible = true;
                    errorMessageLabel.Text = "This account has been registed.";
                }

                else {
                    errorMessageLabel.Visible = true;
                    errorMessageLabel.Text = "Check your network connection.";
                }
            }
        }

        private void guna2Button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
